package cn.dengta.webapp.trade.entity.meta;

import cn.dengta.webapp.trade.entity.meta.MetaConfig.MetaRuleConfig;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.*;

/**
 * 直播主播等级的规则配置对象
 */
@Getter
@Setter
public class LiveAnchorAssessRule implements MetaRuleConfig {

	/** 最低满足条件数 */
	public Integer minCount;
	public LiveAnchorEntry[] entries;

	@Override
	public void validate() {
		final LiveAnchorEntry[] entries = this.entries;
		Assert.isTrue(X.size(entries) > 0, "直播主播配置不能为空！");
		Assert.isTrue(minCount > 0 && minCount <= 6, "满足条件数 > 0 && <= 6");
		for (int i = entries.length - 1; i > 0; ) {
			entries[i--].validate();
		}
	}

	@Override
	public void init(MetaRuleConfig config) {
		LiveAnchorAssessRule rule = (LiveAnchorAssessRule) config;
		this.minCount = rule.minCount;
		this.entries = rule.entries;
	}

	@JSONField(serialize = false)
	public LiveAnchorEntry getEntry(LiveAnchorEntry input, boolean defaultValue) {
		final LiveAnchorEntry[] entries = this.entries;
		for (LiveAnchorEntry entry : entries) { // 等级 大到小
			int index = 0;
			if (input.digTimes >= entry.digTimes) {
				index++;
			}
			if (input.commentTimes >= entry.commentTimes) {
				index++;
			}
			if (input.relayTimes >= entry.relayTimes) {
				index++;
			}
			if (input.userCount >= entry.userCount) {
				index++;
			}
			if (input.newFollowCount >= entry.newFollowCount) {
				index++;
			}
			if (input.contactTimes >= entry.contactTimes) {
				index++;
			}
			if (index >= minCount) {
				return entry;
			}
		}
		return defaultValue ? entries[entries.length - 1] : null;
	}

	/**
	 * 直播主播规则实体【等级大 -> 小】
	 */
	@Setter
	@Getter
	@Accessors(chain = true)
	public static class LiveAnchorEntry {

		/** 名称 */
		protected String name;
		/** 累计点赞量 */
		protected Long digTimes;
		/** 累计评论量 */
		protected Long commentTimes;
		/** 累计转发量 */
		protected Integer relayTimes;
		/** 累计直播间观众人数 */
		protected Integer userCount;
		/** 累计新增喜欢数 */
		protected Integer newFollowCount;
		/** 累计互动量 */
		protected Integer contactTimes;

		public void validate() {
			Assert.isTrue(StringUtil.notEmpty(name), "名称不能为空！");
			Assert.isTrue(digTimes >= 0, "累计点赞量不能小于0！");
			Assert.isTrue(commentTimes >= 0, "累计评论量不能小于0！");
			Assert.isTrue(relayTimes >= 0, "累计转发量不能小于0！");
			Assert.isTrue(userCount >= 0, "累计观众人数不能小于0！");
			Assert.isTrue(newFollowCount >= 0, "累计新增喜欢数不能小于0！");
			Assert.isTrue(contactTimes >= 0, "累计互动量不能小于0！");
		}

	}

}
